﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using Com.Toook.Common;

public partial class nst_RssSitesAdd : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            InitData();
        }
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        if(HaveSameUrl(txtUrl.Text.Trim()))
        {
            LabelMsg.Text = "此url已经存在";
            return;
        }
        string sql = "insert into RssSites(SiteId, SiteName, IsValid, Url, NewsCount, ExpiryTime, TimeSpan, ErrorN, haveContent, haveImg, newsType, xmlSiteId, xmlFileName) " +
            " values(@SiteId, @SiteName, @IsValid, @Url, @NewsCount, @ExpiryTime, @TimeSpan, @ErrorN, @haveContent, @haveImg, @newsType, @xmlSiteId, @xmlFileName)";
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.Parameters.Add("@SiteId", OleDbType.Integer).Value = txtSiteId.Text.Trim();
        cmd.Parameters.Add("@SiteName", OleDbType.VarWChar, 20).Value = txtSiteName.Text;
        cmd.Parameters.Add("@IsValid", OleDbType.Integer).Value = CheckBoxIsValid.Checked ? 1 : 0;
        cmd.Parameters.Add("@Url", OleDbType.VarChar, 200).Value = txtUrl.Text;
        cmd.Parameters.Add("@NewsCount", OleDbType.Integer).Value = txtNewsCount.Text;
        cmd.Parameters.Add("@ExpiryTime", OleDbType.Date).Value = DateTime.Now;
        cmd.Parameters.Add("@TimeSpan", OleDbType.Integer).Value = txtTimeSpan.Text;
        cmd.Parameters.Add("@ErrorN", OleDbType.Integer).Value = ddlErrorN.SelectedValue;
        cmd.Parameters.Add("@haveContent", OleDbType.Integer).Value = CheckBoxHaveContent.Checked ? 1 : 0;
        cmd.Parameters.Add("@haveImg", OleDbType.Integer).Value = CheckBoxHaveImg.Checked ? 1 : 0;
        cmd.Parameters.Add("@newsType", OleDbType.Integer).Value = ddlType.SelectedValue;
        cmd.Parameters.Add("@xmlSiteId", OleDbType.VarChar, 20).Value = txtXmlSiteId.Text;
        cmd.Parameters.Add("@xmlFileName", OleDbType.VarChar, 30).Value = txtXmlFileName.Text;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            LabelMsg.Text = "添加成功" + txtSiteId.Text;
            txtUrl.Text = "";
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            AccessHelper.CloseDbResource(con, null);
        }
        int siteId;
        int.TryParse(txtSiteId.Text, out siteId);
        txtSiteId.Text = (siteId + 1).ToString();
    }
    private bool HaveSameUrl(string url)
    {
        bool have = false;
        string sql = "select count(1) from RssSites where url=@url";
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.Parameters.Add("@url", OleDbType.VarChar, 200).Value = url;        
        try
        {
            con.Open();
            object count = cmd.ExecuteScalar();
            have = count.ToString() != "0";
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            AccessHelper.CloseDbResource(con, null);
        }
        return have;
    }
    private void InitData()
    {
        string sql = "select max(SiteId) from RssSites ";
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        int maxId = 0;
        try
        {
            con.Open();
            object id = cmd.ExecuteScalar();
            if(id != null)
            {
                int.TryParse(id.ToString(), out maxId);
                txtSiteId.Text = (maxId + 1).ToString();
            }             
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            AccessHelper.CloseDbResource(con, null);
        }
    }
}
